package com.xzakota.minesweeper.db;

import com.xzakota.minesweeper.log.XLogger;
import com.xzakota.minesweeper.model.entity.UserBean;
import com.xzakota.minesweeper.msg.MsgCode;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * user 数据库访问实现
 *
 * @author Xzakota
 */
public class UserDaoImpl {
    /**
     * 通过用户名查找用户
     *
     * @param username     用户名
     * @param isPwdVisible 密码是否可见
     * @return 用户 UserBean 对象
     */
    public static UserBean findUser(String username, boolean isPwdVisible) {
        Connection conn = ConnectionManager.getConnection();
        String sql = "SELECT * FROM user WHERE username = ?";
        PreparedStatement pStatement = null;
        ResultSet resultSet = null;
        try {
            pStatement = conn.prepareStatement(sql);
            pStatement.setString(1, username);
            resultSet = pStatement.executeQuery();
            if (!resultSet.next()) {
                XLogger.info("未找到用户 " + username);
                return null;
            } else {
                UserBean userBean = new UserBean();
                userBean.username = resultSet.getString("username");
                if (isPwdVisible) {
                    userBean.password = resultSet.getString("password");
                }
                userBean.qq = resultSet.getString("qq");
                userBean.score = resultSet.getInt("score");
                userBean.online = resultSet.getInt("online");
                return userBean;
            }
        } catch (SQLException e) {
            XLogger.error(e);
            return null;
        } finally {
            ConnectionManager.release(resultSet, pStatement, conn);
        }
    }

    /**
     * 用户登录
     *
     * @param user 用户
     * @return 结果码
     */
    public static int signIn(UserBean user) {
        UserBean find = findUser(user.username, true);
        if (find == null) {
            XLogger.warn(user.username + " 用户不存在！");
            return MsgCode.SIGN_IN_USER_NOT_EXIST;
        }

        Connection conn = ConnectionManager.getConnection();
        PreparedStatement pStatement = null;
        String onlineSQL = "UPDATE user SET online = ? WHERE username = ?";
        try {
            if (user.password.equals(find.password)) {
                if (find.online == 1) {
                    XLogger.warn(user.username + " 用户已在线！");
                    return MsgCode.SIGN_IN_USER_ONLINE;
                }
                user.online = 1;
                pStatement = conn.prepareStatement(onlineSQL);
                pStatement.setInt(1, 1);
                pStatement.setString(2, user.username);
                pStatement.executeUpdate();
                XLogger.info(user.username + " 用户登录成功！");
                return MsgCode.BASE_SUCCESS;
            } else {
                XLogger.warn(user.username + " 用户密码错误！");
                return MsgCode.SIGN_IN_USER_ERROR_PASSWORD;
            }
        } catch (Exception e) {
            XLogger.error(e);
            return MsgCode.BASE_FAIL;
        } finally {
            ConnectionManager.release(null, pStatement, conn);
        }
    }

    /**
     * 用户注册
     *
     * @param user 用户
     * @return 结果码
     */
    public static int signUp(UserBean user) {
        UserBean find = findUser(user.username, false);
        if (find != null) {
            XLogger.warn(user.username + " 用户已存在！");
            return MsgCode.SIGN_UP_USER_EXIST;
        }
        if (user.username.length() > 10 || user.username.length() < 5) {
            return MsgCode.SIGN_UP_ERROR_PARAMETER;
        }
        if (user.password.length() > 10 || user.password.length() < 6) {
            return MsgCode.SIGN_UP_ERROR_PARAMETER;
        }
        if (user.qq.length() > 10 || user.qq.length() < 6) {
            return MsgCode.SIGN_UP_ERROR_PARAMETER;
        }
        Connection conn = ConnectionManager.getConnection();
        String signUpSQL = "INSERT INTO user VALUES (?,?,?,?,?)";
        PreparedStatement pStatement = null;
        try {
            pStatement = conn.prepareStatement(signUpSQL);
            pStatement.setString(1, user.username);
            pStatement.setString(2, user.password);
            pStatement.setString(3, user.qq);
            pStatement.setInt(4, 0);
            pStatement.setInt(5, 0);
            pStatement.executeUpdate();
            XLogger.info(user.username + " 用户注册成功！");
            return MsgCode.BASE_SUCCESS;
        } catch (SQLException e) {
            XLogger.error(e);
            return MsgCode.BASE_FAIL;
        } finally {
            ConnectionManager.release(null, pStatement, conn);
        }
    }

    /**
     * 用户登出
     *
     * @param user 用户
     * @return 结果码
     */
    public static int signOut(UserBean user) {
        UserBean find = findUser(user.username, false);
        if (find == null) {
            XLogger.warn(user.username + " 用户不存在！");
            return MsgCode.SIGN_OUT_USER_NOT_EXIST;
        }

        Connection conn = ConnectionManager.getConnection();
        String signOutSQL = "update user set online = ? where username = ?";
        PreparedStatement pStatement = null;
        try {
            pStatement = conn.prepareStatement(signOutSQL);
            pStatement.setInt(1, 0);
            pStatement.setString(2, user.username);
            pStatement.executeUpdate();
            XLogger.info(user.username + " 用户登出成功！");
            return MsgCode.BASE_SUCCESS;
        } catch (SQLException e) {
            XLogger.error(e);
            return MsgCode.BASE_FAIL;
        } finally {
            ConnectionManager.release(null, pStatement, conn);
        }
    }
}
